package com.dxf.sysadmin.service.impl;

import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxf.common.utils.ResultJson;
import com.dxf.sysadmin.entity.EvaluteItem;
import com.dxf.sysadmin.entity.EvaluteTarget;
import com.dxf.sysadmin.entity.EvaluteType;
import com.dxf.sysadmin.mapper.EvaluteTargetMapper;
import com.dxf.sysadmin.service.EvaluteTargetService;
import com.dxf.sysadmin.service.EvaluteTypeService;
import com.dxf.sysadmin.utils.DeptTree;
import com.dxf.sysadmin.utils.SelectTree;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 评价指标 服务实现类
 */
@Service
public class EvaluteTargetServiceImpl extends ServiceImpl<EvaluteTargetMapper, EvaluteTarget> implements EvaluteTargetService {
    List<EvaluteTarget> treeTable;
    @Autowired
    EvaluteTypeService evaluteTypeService;
    /**
     * 评价指标列表查询
     */
    @Override
    public ResultJson targetPage() {
        treeTable= new ArrayList<>();
        //获取顶级指标
        List<EvaluteTarget> listOne =list(new QueryWrapper<EvaluteTarget>().
                lambda().eq(EvaluteTarget::getParentId, 0));
        listOne.forEach(x -> {
            treeTable.add(x);
            getTargetChild(x);
        });
        return ResultJson.ok().data("treeTable",treeTable);
    }

    /**
     *更具id列表删除评价指标
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson deleteTarget(Integer id) {
        removeById(id);
        // TODO 删除所有子级
        return ResultJson.ok();
    }

    /**
     *新增或者更新评价指标
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson saveOrUpdateTarget(EvaluteTarget target) {
        saveOrUpdate(target);
        return ResultJson.ok();
    }

    /**
     *评价指标下拉选项
     */
    @Override
    public ResultJson targetSelect() {
        List<SelectTree> treeTable = new ArrayList<>();
        //类型列表
        List<EvaluteType> list = evaluteTypeService.list();
        List<EvaluteTarget> allTarget=list();
        list.forEach(x->{
            SelectTree one = new SelectTree();
            BeanUtils.copyProperties(x, one);
            //当前指标类型列表
            List<EvaluteTarget> findTarget = allTarget.stream().filter(m-> m.getTypeId().equals(x.getId())).collect(Collectors.toList());
            //父级指标及子级树
            List<SelectTree> children= new ArrayList<>();
            List<EvaluteTarget> first = findTarget.stream().filter(m-> m.getParentId().equals(0L)).collect(Collectors.toList());
            for (EvaluteTarget target:first){
                children.add(convertToNode(target,findTarget));
            }
            one.setChildren(children);
            treeTable.add(one);
        });
        return ResultJson.ok().data("selectTree",treeTable);
    }

    /**
     * @Description: 更具当前类型指标集合生成树
     * @Param parentId 父级id
     * @Param all 更具当前类型指标集合
     * @Return: java.util.List<com.dxf.sysadmin.utils.SelectTree>
     */
    private  List<SelectTree> generateTree(Long parentId,List<EvaluteTarget> all){
        //类型下的子级
        List<SelectTree> ret=new ArrayList<>();
        //
        List<EvaluteTarget> first=all.stream().filter(x->x.getParentId().equals(parentId)).collect(Collectors.toList());
        for (EvaluteTarget target:first){
            SelectTree node=new SelectTree();
            BeanUtils.copyProperties(target,node);
            node.setChildren(generateTree(target.getId(),all));
            ret.add(node);
        }
        return ret;
    }

    /**
     *递归调用获取子级
     */
    private void getTargetChild(EvaluteTarget parent){
        List<EvaluteTarget> listTwo =list(new QueryWrapper<EvaluteTarget>().
                lambda().eq(EvaluteTarget::getParentId, parent.getId()));
        listTwo.forEach(x->{
            treeTable.add(x);
            getTargetChild(x);
        });
    }

    /**
     *更具当前指标生成对应树
     */
    private SelectTree convertToNode(EvaluteTarget target,List<EvaluteTarget> all){
        SelectTree node=new SelectTree();
        BeanUtils.copyProperties(target,node);

        List<SelectTree> nodes=new ArrayList<>();
        List<EvaluteTarget> children=all.stream().filter(x->x.getParentId().equals(target.getId())).collect(Collectors.toList());
        for(EvaluteTarget child :children){
            nodes.add(convertToNode(child,all));
        }
        if (nodes.size()!=0){
            node.setChildren(nodes);
        }
        return node;
    }
}
